package com.yjc.hire.work.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yjc.bo.EditWorkExpBO;
import com.yjc.hire.base.BaseInfoProperties;
import com.yjc.hire.work.bo.EditProjectExpBO;
import com.yjc.hire.work.bo.EditResumeBO;
import com.yjc.hire.work.mapper.ResumeMapper;
import com.yjc.hire.work.mapper.ResumeProjectExpMapper;
import com.yjc.hire.work.mapper.ResumeWorkExpMapper;
import com.yjc.hire.work.service.ResumeService;
import com.yjc.hire.work.vo.ResumeVO;
import com.yjc.pojo.Admin;
import com.yjc.pojo.Resume;
import com.yjc.pojo.ResumeProjectExp;
import com.yjc.pojo.ResumeWorkExp;
import jodd.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * </p>
 *
 * @author jcyin
 * @since 2025/7/19
 * </p>
 */

@Service
public class ResumerServiceImpl extends ServiceImpl<ResumeMapper, Resume> implements ResumeService {
    @Resource
    private ResumeMapper resumeMapper;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private ResumeWorkExpMapper resumeWorkExpMapper;

    @Autowired
    private ResumeProjectExpMapper resumeProjectExpMapper;

    private static final String REDIS_RESUME_INFO = "redis_resume_info";
    @Transactional
    @Override
    public void initResume(String userId) {
        Resume resume = new Resume();
        resume.setUserId(userId);
        resume.setCreateTime(LocalDateTime.now());
        resume.setUpdatedTime(LocalDateTime.now());
        resumeMapper.insert(resume);

    }

    @Transactional
    @Override
    public void modifyResume(EditResumeBO editResumeBO) {
        Resume resume = new Resume();
        BeanUtils.copyProperties(editResumeBO, resume);
        resume.setUpdatedTime(LocalDateTime.now());
        resumeMapper.update(resume,
                new LambdaQueryWrapper<Resume>().eq(Resume::getUserId, editResumeBO.getUserId())
                        .eq(Resume::getId, editResumeBO.getId()));
        stringRedisTemplate.delete(REDIS_RESUME_INFO + ":" + editResumeBO.getUserId());

    }

    @Override
    public ResumeVO getResumeInfo(String userId) {
        ResumeVO resumeVO = new ResumeVO();
        Resume resume = resumeMapper.selectOne(new LambdaQueryWrapper<Resume>().eq(Resume::getUserId, userId));
        if (resume == null) return null;
        BeanUtils.copyProperties(resume, resumeVO);
        //查询工作经验
        List<ResumeWorkExp> resumeWorkExps = resumeWorkExpMapper.selectList(new LambdaQueryWrapper<ResumeWorkExp>()
                .eq(ResumeWorkExp::getUserId, userId)
                .eq(ResumeWorkExp::getResumeId, resume.getId()));
        resumeVO.setWorkExpList(resumeWorkExps);

        return resumeVO;
    }

    /**
     * 新增或者修改工作经验
     */
    @Override
    public void editWorkExp(EditWorkExpBO editWorkExpBO) {
        ResumeWorkExp workExp = new ResumeWorkExp();
        BeanUtils.copyProperties(editWorkExpBO, workExp);
        workExp.setUpdatedTime(LocalDateTime.now());
        //新增操作
        if (StringUtils.isBlank(workExp.getId())) {
            workExp.setCreateTime(LocalDateTime.now());
            resumeWorkExpMapper.insert(workExp);
        } else {
            resumeWorkExpMapper.update(
                    workExp, new LambdaQueryWrapper<ResumeWorkExp>()
                            .eq(ResumeWorkExp::getId, editWorkExpBO.getId())
                            .eq(ResumeWorkExp::getUserId, editWorkExpBO.getUserId())
                            .eq(ResumeWorkExp::getResumeId, editWorkExpBO.getResumeId())
            );
        }
        stringRedisTemplate.delete(REDIS_RESUME_INFO + ":" + editWorkExpBO.getUserId());
    }

    @Override
    public ResumeWorkExp getWorkExp(String workWxpId, String userId) {
        return resumeWorkExpMapper.selectOne(
                new LambdaQueryWrapper<ResumeWorkExp>().eq(ResumeWorkExp::getUserId, userId)
                        .eq(ResumeWorkExp::getId, workWxpId)
        );
    }

    @Override
    public void deleteWorkExp(String workWxpId, String userId) {
        resumeWorkExpMapper.deleteWorkExp(workWxpId,userId);
    }

    @Override
    @Transactional
    public void editProjectExp(EditProjectExpBO editProjectExpBO) {
        ResumeProjectExp exp = new ResumeProjectExp();
        BeanUtils.copyProperties(editProjectExpBO, exp);
        exp.setUpdatedTime(LocalDateTime.now());
        if (StringUtils.isBlank(exp.getId())) {
            //新增
            exp.setCreateTime(LocalDateTime.now());
            resumeProjectExpMapper.insert(exp);
        }
        else {
            resumeProjectExpMapper.update(exp,
                    new LambdaQueryWrapper<ResumeProjectExp>().eq(ResumeProjectExp::getId, exp.getId())
                            .eq(ResumeProjectExp::getUserId, editProjectExpBO.getUserId())
                    .eq(ResumeProjectExp::getResumeId, exp.getResumeId())
            );
        }
        stringRedisTemplate.delete(REDIS_RESUME_INFO + ":" + editProjectExpBO.getUserId());
    }

    @Override
    public ResumeProjectExp getProjectExp(String projectExpId, String userId) {
        return resumeProjectExpMapper.selectOne(
                new LambdaQueryWrapper<ResumeProjectExp>().eq(ResumeProjectExp::getUserId, userId)
                .eq(ResumeProjectExp::getId, projectExpId)
        );
    }

    @Override
    @Transactional
    public void resumeRefresh(String resumeId, String userId) {
        EditResumeBO resumeBO = new EditResumeBO();
        resumeBO.setUserId(userId);
        resumeBO.setId(resumeId);
        resumeBO.setRefreshTime(LocalDateTime.now());
        modifyResume(resumeBO);
    }
}
